﻿using System;
using System.Windows.Forms;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Biz;
using QuanLyThuPhiTaiLieu.Common;
using QuanLyThuPhiTaiLieu.Entity;
using System.IO;

namespace QuanLyThuPhiTaiLieu.Forms
{
    public partial class NewThamKhaoTaiLieu : Form
    {
        private int id;

        public NewThamKhaoTaiLieu()
        {
            InitializeComponent();
        }

        public NewThamKhaoTaiLieu(int id)
        {
            InitializeComponent();

            this.id = id;                       
        }

        private void SaveItem(object sender, EventArgs e)
        {
            if (Validate())
            {
                string message;

                var biz = new ThamKhaoTaiLieuBiz();

                var item = new ThamKhaoTaiLieu
                {                   
                    MaTaiLieu  =textMaTaiLieu.Text,
                    LoaiTaiLieu = textLoaiTaiLieu.Text,
                    DonViTinh = textDonViTinh.Text,
                    Phi = Utilities.GetNumberFieldValue(numberPhi.Value)
                };

                if (id > 0)
                {
                    item.Id = id;
                    biz.UpdateItem(item);

                    message = Constants.Messages.UpdateCompleted;
                }
                else
                {
                    biz.SaveItem(item);
                    message = Constants.Messages.InsertCompleted;
                }

                MessageBox.Show(message);
                Utilities.CloseForm(this, new ViewThamKhaoTaiLieu());
            }
        }
        
        private void Cancel(object sender, EventArgs e)
        {
            Utilities.CloseForm(this, new ViewThamKhaoTaiLieu());
        }

        private new bool Validate()
        {
            bool hasError = true;
            bool isFocused = false;
            errorProvider.Clear();

            if (string.IsNullOrEmpty(textMaTaiLieu.Text))
            {
                errorProvider.SetError(textMaTaiLieu, Constants.Messages.RequireMessage);
                hasError = false;

                textMaTaiLieu.Focus();
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textLoaiTaiLieu.Text))
            {
                errorProvider.SetError(textLoaiTaiLieu, Constants.Messages.RequireMessage);
                hasError = false;

                if (!isFocused)
                {
                    textLoaiTaiLieu.Focus();
                }
                isFocused = true;
            }

            if (string.IsNullOrEmpty(textDonViTinh.Text))
            {
                errorProvider.SetError(textDonViTinh, Constants.Messages.RequireMessage);
                hasError = false;
                if (!isFocused)
                {
                    textDonViTinh.Focus();
                }
            }

            return hasError;
        }

        private void FormLoad(object sender, EventArgs e)
        {
            var parent = MdiParent as MDI;
            if (parent != null)
            {
                //Utilities.ResetBackColor(parent.TopMenu, Constants.MenuIndex.TaiLieuVanBanPhapLy);
                parent.Status.Text = Constants.FormStatus.ThamKhaoTaiLieu;

                if (parent.CurrentGuest != null)
                {
                    groupData.Enabled = false;
                    buttonSave.Visible = false;
                }
            }

            if (id > 0)
            {
                var biz = new ThamKhaoTaiLieuBiz();
                var item = biz.LoadItem(id);

                textMaTaiLieu.Text = Convert.ToString(item.MaTaiLieu);
                textLoaiTaiLieu.Text = Convert.ToString(item.LoaiTaiLieu);
                textDonViTinh.Text = Convert.ToString(item.DonViTinh);
                numberPhi.Value = item.Phi;               
            }

            textMaTaiLieu.Focus();
        }
        
    }
}
